using System.Collections;
using UnityEngine;

public class AICharacter : Character
{
	private enum Mark
	{
		None = 0,
		Killer = 1,
		Target = 2
	}

	private Vector2 displacement;

	private AIPerception aiPerception;

	private float waitForBoost;

	private float boostUntil;

	private bool isHunter;

	public SpriteRenderer markSpriteRenderer;

	public Sprite targetSprite;

	public Sprite dangerSprite;

	private Mark currentMark;

	private Mark nextMark;

	public bool IsHunter
	{
		get
		{
			return isHunter;
		}
	}

	private int MaxSpeed
	{
		get
		{
			return (!GameManager.Instance.PlayerCharacter.IsAlive || GameManager.Instance.PlayerCharacter.Mass <= 13000) ? Random.Range(7000, 15000) : (GameManager.Instance.PlayerCharacter.Mass + Random.Range(-3000, 3000));
		}
	}

	protected override void SetMass(int value)
	{
		if (GameManager.Instance.PlayerCharacter.Rank <= 2 && value < GameManager.Instance.PlayerCharacter.Mass)
		{
			if (rank <= 3)
			{
				value += 400;
			}
			else if (rank < 6)
			{
				value += 200;
			}
		}
		base.SetMass(value);
	}

	protected override void Awake()
	{
		base.Awake();
		aiPerception = perception as AIPerception;
	}

	public override void Initialize(int index, Vector3 position, int mass)
	{
		base.Initialize(index, position, mass);
		currentMark = Mark.None;
		nextMark = Mark.None;
		markSpriteRenderer.gameObject.SetActive(false);
		type = index % GameManager.Instance.FidgetStore.fidgetTypes.Count;
		int num = MaxSpeed;
		if (index < 3)
		{
			base.Mass = num;
			isHunter = true;
		}
		else if (index < 6)
		{
			base.Mass = Random.Range(num / 5, num / 2);
			isHunter = true;
		}
		else if (index < 10)
		{
			base.Mass = Random.Range(num / 15, num / 5);
		}
		else
		{
			base.Mass = Random.Range(12, num / 15);
		}
		base.name = Values.Instance.names[Random.Range(0, Values.Instance.names.Count)];
		nameText.text = base.name;
		waitForBoost = Time.time + (float)Random.Range(20, 40);
	}

	protected override void FixedUpdate()
	{
		if (base.IsAlive)
		{
			if (!base.IsHitted)
			{
				moveDirection = Vector3.Slerp(moveDirection, moveDirection + aiPerception.GetDirection(), 0.5f);
				moveDirection.Normalize();
			}
			if (!base.BoostEnabled && (float)base.Mass > 13f && Time.time < boostUntil)
			{
				base.BoostEnabled = true;
			}
			else if (Time.time > boostUntil && base.BoostEnabled)
			{
				base.BoostEnabled = false;
				if (waitForBoost < Time.time)
				{
					waitForBoost = Time.time + Random.Range(15f, 40f);
				}
			}
			if ((float)base.Mass > 13f)
			{
				if (!base.BoostEnabled && Time.time > waitForBoost)
				{
					boostUntil = Time.time + Random.Range(3f, 6f);
				}
			}
			else
			{
				base.BoostEnabled = false;
			}
			if (GameManager.Instance.PlayerCharacter.IsAlive && FollowingCamera.instance.IsInsideOfCamera2D(base.Position))
			{
				if ((float)GameManager.Instance.PlayerCharacter.Mass * Values.Instance.BigDamageRatio >= (float)base.Mass)
				{
					nextMark = Mark.Target;
				}
				else if ((float)base.Mass * Values.Instance.BigDamageRatio >= (float)GameManager.Instance.PlayerCharacter.Mass)
				{
					nextMark = Mark.Killer;
				}
				else
				{
					nextMark = Mark.None;
				}
			}
			else
			{
				nextMark = Mark.None;
			}
			if (currentMark != nextMark)
			{
				if (currentMark == Mark.None && nextMark != 0)
				{
					markSpriteRenderer.gameObject.SetActive(true);
				}
				else if (nextMark == Mark.None)
				{
					markSpriteRenderer.gameObject.SetActive(false);
				}
				currentMark = nextMark;
				switch (currentMark)
				{
				case Mark.Killer:
					markSpriteRenderer.sprite = dangerSprite;
					break;
				case Mark.Target:
					markSpriteRenderer.sprite = targetSprite;
					break;
				default:
					markSpriteRenderer.sprite = null;
					break;
				}
			}
		}
		base.FixedUpdate();
	}

	private void Steer()
	{
		Vector2 zero = Vector2.zero;
		zero += Steering.Wander(moveDirection, base.Position, speed, 1f, 1f, 60f, ref displacement);
		zero += Steering.RunAwayFromBorder(moveDirection, base.Position, speed);
		zero *= 0.14f;
		moveDirection += zero;
		moveDirection.Normalize();
	}

	protected override IEnumerator KillRoutine()
	{
		DisablePhysics();
		anim.Play("DieAnim");
		yield return new WaitForSeconds(Values.Instance.DieAnimDuration);
		GameManager.Instance.AddDeadCharacter(this);
		base.transform.position = new Vector3(1000f, -1000f);
		base.transform.parent.gameObject.SetActive(false);
	}

	public void OpenBoost()
	{
		boostUntil = Time.time + Random.Range(3f, 6f);
	}
}
